<template>
  <div>
    <!-- 学院管理 -->

    <!-- 表单 -->
    <el-form size="small" :model="form" :inline="true">
      <el-form-item label="院系" style="font-weight:bold;">
        <el-select v-model="form.name" placeholder="请选择院系" style="width:200px;" clearable>
          <el-option v-for="item in CollegeName" :key="item.id" :label="item.name" :value="item.name"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状 态" style="font-weight:bold;">
        <el-select v-model="form.state" placeholder="请选择状态" style="width:200px;" clearable>
          <el-option label="正常" value="0"></el-option>
          <el-option label="停用" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-button size="small" class="newly_button" type="primary" icon="el-icon-folder-add"
        @click="dialog_newly = true">新增</el-button>
      <el-button size="small" class="search_button" type="primary" icon="el-icon-search"
        @click="getDataList">查询</el-button>
      <el-button size="small" class="reset_button" icon="el-icon-refresh" @click="reset">重置</el-button>
    </el-form>
    <!-- 表单 -->
    <el-row>
      <!-- <el-button size="small" class="newly_button" type="primary" icon="el-icon-folder-add" @click="dialog_newly = true">新增</el-button> -->
      <!-- <el-button type="danger" icon="el-icon-delete">删除</el-button> -->
    </el-row>

    <el-divider></el-divider>
    <!-- 表格 -->
    <el-table size="medium" ref="multipleTable" :data="tableData" tooltip-effect="dark" style="width: 100%"
      @selection-change="handleSelectionChange" border>
      <!-- <el-table-column type="selection" width="50" align="center"></el-table-column> -->
      <!-- <el-table-column prop="id" label="ID" align="center"></el-table-column> -->
      <el-table-column prop="name" label="院系" align="center"></el-table-column>
      <el-table-column prop="state" label="状态" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.state == 0" style="color:#67C23A;">正常</span>
          <span v-if="scope.row.state == 1" style="color:red;">停用</span>
        </template>
      </el-table-column>
      <el-table-column prop="handle" label="操作" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" plain @click="updateCollege(scope.row)">修改</el-button>
          <el-button size="mini" type="danger" plain @click="deleteCollege({ id: scope.row.id })">删除</el-button>
        </template>
      </el-table-column>
    </el-table>


    <el-pagination class="pagination" background layout="total, sizes, prev, pager, next, jumper" :total="total"
      :page-size="10" :page-sizes="[10, 20, 30, 40]" @current-change="handleCurrentChange"
      @size-change="handleSizeChange"></el-pagination>


    <!-- 添加院系弹窗 -->
    <el-dialog title="添加院系" :visible.sync="dialog_newly">
      <el-form :model="formNew" :label-position="labelPosition" label-width="100px" style="margin-left:20%;">
        <el-form-item label="院系名称 :">
          <el-input v-model="formNew.name" style="width:60%" placeholder="请输入院系名称" clearable></el-input>
        </el-form-item>
        <el-form-item label="状态 :">
          <el-select v-model="formNew.state" placeholder="请选择状态" style="width:60%" clearable>
            <el-option label="正常" value="0"></el-option>
            <el-option label="停用" value="1"></el-option>
          </el-select>
          <!-- <el-radio-group v-model="formNew.state">
            <el-radio label="正常" value="0"></el-radio>
            <el-radio label="停用" value="1"></el-radio>
          </el-radio-group> -->
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialog_newly = false">取 消</el-button>
        <el-button type="primary" @click="addCollege" class="dialogbutton">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 添加院系弹窗 -->

    <!-- 修改院系信息弹窗 -->
    <el-dialog title="修改院系信息" :visible.sync="dialog_change">
      <el-form :model="formChange" :label-position="labelPosition" label-width="100px" style="margin-left:20%;">
        <el-form-item label="院系名称 :">
          <el-input v-model="formChange.name" style="width:60%" placeholder="请输入院系名称" clearable></el-input>
        </el-form-item>
        <el-form-item label="状态 :">
          <el-select v-model="formChange.state" placeholder="请选择状态" style="width:60%" clearable>
            <el-option label="正常" value="0"></el-option>
            <el-option label="停用" value="1"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialog_change = false">取 消</el-button>
        <el-button type="primary" @click="updateCollegeConfirm" class="dialogbutton">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 修改校区信息弹窗 -->
  </div>
</template>

<script>
import { getCollegeList, addCollege, updateCollege, deleteCollege, getCollegeName } from '_api/DormManage/College/index.js'
export default {
  data() {
    return {
      dialog_newly: false,
      dialog_change: false,
      labelPosition: "left",
      form: {
        current: 0,
        field: "",
        name: "",
        order: "",
        pageSize: 10,
        state: ""
      },
      formNew: {
        name: "",
        state: ""
      },
      formChange: {},
      tableData: [],
      CollegeName: [],
      total: 0
    }
  },
  methods: {
    handleSelectionChange() {

    },
    //重置
    reset() {
      this.$router.go(0)
    },
    // 删除学院
    deleteCollege(e) {
      this.$confirm('此操作将删除该学院, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteCollege(e).then(res => {
          // console.log(res)
          if (res.status == 200) {
            this.$message({
              message: '删除成功!',
              duration: 1000,
              type: 'success',
              onClose: () => {
                this.$router.go(0)
              }
            })
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    //修改院系弹窗
    updateCollege(e) {
      this.dialog_change = true
      this.formChange = e
      // console.log(e)
    },
    //修改院系确认
    updateCollegeConfirm() {
      updateCollege(this.formChange).then(res => {
        // console.log(res)
        if (res.status == 200) {
          this.dialog_change = false
          this.$message({
            message: '修改成功!',
            duration: 1000,
            type: 'success',
            onClose: () => {
              this.$router.go(0)
            }
          })
        }
      })
    },
    // 添加院系
    addCollege() {
      addCollege(this.formNew).then(res => {
        // console.log(res)
        if (res.status == 200) {
          this.dialog_newly = false
          this.$message({
            message: '添加成功!',
            duration: 1000,
            type: 'success',
            onClose: () => {
              this.$router.go(0)
            }
          })
        }
      })
    },
    // prev(e) {
    //   //上一页
    //   this.form.current = e-1
    //   this.getCollegeList()
    // },
    // next(e) {
    //   //下一页
    //   this.form.current = e-1
    //   this.getCollegeList()
    // },
    handleCurrentChange(e) {
      //当前页
      this.form.current = e
      this.getCollegeList()
    },
    handleSizeChange(e) {
      this.form.pageSize = e
      this.getCollegeList()
    },
    // 查询按钮
    getDataList() {
      this.form.current = 0
      this.getCollegeList()
    },
    // 查询院系
    getCollegeList() {
      getCollegeList(
        this.form
      ).then(res => {
        // console.log(res);
        if (res.status == 200) {
          this.getCollegeName()
          this.tableData = res.data.data.list
          this.total = res.data.data.total
        }
      }).catch(error => {
        // console.log(error.response.status);
        // if (error.response.status == 401) {
        //   this.$confirm('登录状态过期, 请重新登录!', '提示', {
        //     confirmButtonText: '确定',
        //     // cancelButtonText: '取消',
        //     showCancelButton: false,
        //     type: 'warning'
        //   }).then(() => {
        //     this.$router.push('/login')
        //   }).catch(() => {

        //   })
        // }
      })
    },
    //获取学院名称
    getCollegeName() {
      getCollegeName().then(res => {
        // console.log(res)
        if (res.status == 200) {
          this.CollegeName = res.data.data
        }
      })
    }
  },
  created() {
    this.getCollegeList()
    // this.getCollegeName()
  },
};
</script>
<style lang="scss" scoped>
.search_button {
  margin-left: 80px;
}

.reset_button {
  margin-left: 10px;
}

.newly_button {
  margin-left: 50px;
}

.pagination {
  float: right;
  margin-top: 10px;
}</style>
